package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.volunteerstar.VolunteerStarAddDto;
import com.teacher.pojo.dto.volunteerstar.VolunteerStarUpdateDto;
import com.teacher.pojo.dto.volunteerstar.VolunteerStartGetDto;
import com.teacher.pojo.entity.VolunteerStar;
import com.teacher.pojo.vo.volunteerstar.VolunteerStartGetVo;
import com.teacher.service.IVolunteerStarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 星级志愿者 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/pc/volunteer-star")
@Api(tags = "星级志愿者")
public class VolunteerStarController {

    @Autowired
    private IVolunteerStarService iVolunteerStarService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加星级志愿者
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加星级志愿者 volunteerStar_add  10->一星 20->二星 30->三星 40->四星 50 -> 五星")
    @PostMapping
    @PreAuthorize("hasAuthority('volunteerStar_add')")
    public CommonResult<String> add(@RequestBody @Validated VolunteerStarAddDto dto) {
        VolunteerStar volunteer = new VolunteerStar();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, volunteer);
        if (iVolunteerStarService.save(volunteer)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新星级志愿者
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新星级志愿者 volunteerStar_update")
    @PutMapping
    @PreAuthorize("hasAuthority('volunteerStar_update')")
    public CommonResult<String> update(@RequestBody @Validated VolunteerStarUpdateDto dto) {
        VolunteerStar volunteer = new VolunteerStar();
        BeanUtils.copyProperties(dto, volunteer);
        if (iVolunteerStarService.updateById(volunteer)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除星级志愿者
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除星级志愿者 volunteerStar_delete")
    @PreAuthorize("hasAuthority('volunteerStar_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iVolunteerStarService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询星级志愿者
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询星级志愿者 volunteerStar_get")
    @PreAuthorize("hasAuthority('volunteerStar_get')")
    public CommonResult<IPage<VolunteerStartGetVo>> getPage(VolunteerStartGetDto dto) {
        return iVolunteerStarService.getPage(dto);
    }

    /**
     * 导入星级志愿者
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入星级志愿者 volunteerStar_upload")
    @PreAuthorize("hasAuthority('volunteerStar_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iVolunteerStarService.upload(file);
    }

}

